package com.prothink.threadproject.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;

/**
 * 生成JWT令牌的工具类
 */
public class JWTToken {
    public static SecretKey key = Jwts.SIG.HS256.key().build();

    /*
    生成JWTToken
     */
    public static String getToken(String account){
        String jws = Jwts.builder().subject(account).signWith(key).compact();
        return jws;
    }
    /**
     * 解析JWTToken
     */
    public static String parseToken(String token){
        try{
            Jws<Claims> jws = Jwts.parser().verifyWith(key).build().parseSignedClaims(token);
            return jws.getPayload().getSubject();
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    public static void main(String[] args) {
        String token = getToken("hanyi");
        System.out.println(token);

        System.out.println(parseToken(token));
    }
}
